<?php

namespace app\api\controller;

use app\common\LogService;
use app\common\model\Notification;
use think\facade\Db;

class Msg extends Base
{
    // 首页消息通知统计
    public function index()
    {
        $msgCount = Db::name('notification')->where(['user_id' => $this->uid, 'is_read' => 0])->count();
        $friendRequestCount = Db::name('friendRequest')->where(['receiver_id' => $this->uid, 'status' => 0])->count();
        return success(['msgCount' => $msgCount, 'friendCount' => $friendRequestCount]);
    }
    public function notification()
    {
        $list = Notification::where(['user_id' => $this->uid])->order('id desc,is_read desc')->paginate(10);
        return success($list);
    }
    public function readAll($id = null)
    {
        if (!empty($id)) {
            $res = Notification::where(['id' => $id, 'user_id' => $this->uid, 'is_read' => 0])->update(['is_read' => 1]);
        } else {
            $res = Notification::where(['user_id' => $this->uid, 'is_read' => 0])->update(['is_read' => 1]);
        }
        LogService::record(
            user_id: getUid(),
            name: '一键阅读消息',
        );
    }
}
